---
title: 'Figures'
author: David Hume
date: '`r format(Sys.time(), "%d %B %Y")`'
output: 
   html_document:
    toc: true
    toc_depth: 3 
editor_options: 
  chunk_output_type: console
---
---

```{r setup, message=FALSE, echo = FALSE}
library(tidyverse)
library(data.table)
library(Hmisc)
library(fasttime)
library(lubridate)
library(stringr)
library(cowplot)
library(car)
library(stargazer)
library(lfe)
library(knitr)
library(RColorBrewer)
library(epiDisplay)
library(scales)
library(xtable)
options(width=200, scipen=10)
knitr::opts_chunk$set(echo = TRUE, cache = FALSE, warning = FALSE, 
                      message = FALSE, cache.lazy = FALSE)

```



```{r echo = FALSE, eval=TRUE}

Time <- Sys.time()

# Set-up

# Switches & setting of variables

# Data set either "All8pc", " "All1pc", "Test", "Render"
analysis.1 <- "Render"
# Filetype for output
# "latex" 
# "html" - inserting into html [htmltools::includeHTML()] or Word docs [Insert/[Text]/Object/Text from file]
# "text" - viewing on screen
filetype.1 <- "Render"


analysis <- ifelse(analysis.1 =="Render", readRDS("./Data/Analysis.rds"), analysis.1)
filetype <- ifelse(filetype.1 =="Render", readRDS("./Data/Filetype.rds"), filetype.1)


if (filetype=="latex") {
  out <- "tex"

  } else if (filetype=="html") {
      out <- "html"

    } else if (filetype=="text") {
      out <- "csv"

      } else {
        stop("latex, html or text")
}


# Plots x-axis
highlimit <- .7
lowlimit <- -.4

# Set variables
fig = 0 # Counter for figures

```

```{r cache=FALSE, eval=TRUE, echo=FALSE}
# From Regression_Both.Rmd
Keep_cols <- c("Property_Id", "Type", "New", "Region", "date.val", "YrQtr", "sold.dummy", "IdxPriceA", "PriceA", "DurationVal", "Peak_price", "DurationPeak", "TUI")




#if (analysis.1!="Render") {
  if (analysis=="All") {
    f <- function(x, pos) subset(x, Type == Type) # i.e. reads in all data because condition always TRUE
      Resold.indexed <-  read_csv_chunked("./Data/Resold.indexed.All.csv", DataFrameCallback$new(f), chunk_size = 2000000)
      Resold.indexed <- as.data.table(Resold.indexed)
      Resold.indexed.regress <- Resold.indexed[, ..Keep_cols]
      Resold.indexed <- NULL
      
      } else {
        Resold.indexed.regress <-fread(paste0("./Data/Resold.indexed.", analysis, ".csv"), select = Keep_cols)
  }
#}



Resold.indexed.regress <- Resold.indexed.regress[,`:=`(Type=factor(Type), 
                                                         New=factor(New), 
                                                         YrQtr=factor(YrQtr),
                                                         Region=factor(Region),
                                                         Property_Id = factor(Property_Id),
                                                         date.val=as.Date(date.val, origin ="1970-01-01"),
                                                       Val_year=factor(year(date.val)),
                                                         Val_qtr=factor(quarter(date.val)))][
                                                           ,`:=`(Type = relevel(Type, "Flat"), New = relevel(New, "Old"))]



cat("DATA:", analysis)

```


```{r cache=FALSE, eval=TRUE, echo=FALSE}

# From Figures.Rmd
Resold.indexed.regress <- Resold.indexed.regress[,`:=`(SoldInPeriod =ifelse(sold.dummy,TRUE,FALSE),
                                                         Unrealgain=round(IdxPriceA-PriceA),
                                                         Peak.unrealgain=round(IdxPriceA-Peak_price))
                                                 ][,`:=`(UnrealgainYN=Unrealgain >=0, # Unrealised gain (True), unrealised loss (False)
                                                           UnrealgainAdj = Unrealgain/100000, 
                                                           Peak.unrealgainYN=Peak.unrealgain >=0,  
                                                           Peak.unrealgainAdj = Peak.unrealgain/100000,
                                                       Peak_unrealgain_pc= Peak.unrealgain/Peak_price*100)
                                                     ][,`:=`(Type=factor(Type), 
                                                             New=factor(New), 
                                                             YrQtr=factor(YrQtr),
                                                             Region=factor(Region),
                                                             date.val=as.Date(date.val, origin ="1970-01-01"),
                                                             #Peak_date_start=as.Date(Peak_date_start, origin ="1970-01-01"),
                                                             Val_qtr=factor(quarter(date.val)),
                                                             Val_year=factor(year(date.val)),
                                                             Return_purchase_pc = 10000000*UnrealgainAdj/PriceA,
                                                             Return_peak_pc = 10000000*Peak.unrealgainAdj/Peak_price)
                                                       ][,`:=`(Type = relevel(Type, "Flat"), New = relevel(New, "Old"))
                                                         ][order(Property_Id, date.val)] 











```





```{r cache=FALSE,  echo= FALSE}



regression_variables <- function(dt, Ref_price, Ref_duration, return) {
  # NB Ref_price needs quotes
  DT <- dt[, .(Ref_price=get(Ref_price), Duration = get(Ref_duration), IdxPriceA, PriceA, Purch_duration = DurationVal, 
               sold.dummy, Property_Id, YrQtr, Val_year, Val_qtr, Type, New, Quality100000, Region, GainPurch, GainPeak, Return_decile, Return_quintile)]
  
  DT[, F_unrealgain :=round(IdxPriceA-Ref_price) # Calculates unrealised gain or loss - price paid vs index
     ][, `:=`(F_unrealgainAdj = Winsorize(F_unrealgain/100000, probs = c(0.01, 0.99), na.rm = TRUE),
              F_unrealgainYN = F_unrealgain >=0,
              Price_unrealgainAdj = Winsorize((IdxPriceA-PriceA)/100000, probs = c(0.01, 0.99), na.rm = TRUE),
              Price_unrealgainYN = ifelse(IdxPriceA-PriceA>=0, TRUE, FALSE))
       ][, `:=`(Purch_duration2 = Purch_duration^2,
                         Purch_duration3 = Purch_duration^3,
                         Purch_duration4 = Purch_duration^4,
                         Purch_duration5 = Purch_duration^5)]
  
  
  
  if (return=="P") {
   DT[, `:=`(F_unrealgain.pos = ifelse(F_unrealgainYN,10000000*F_unrealgainAdj/Ref_price,0),
                       F_unrealgain.neg = ifelse(!F_unrealgainYN,10000000*F_unrealgainAdj/Ref_price,0),
                       Price_unrealgain.pos = ifelse(Price_unrealgainYN,10000000*Price_unrealgainAdj/PriceA,0),
                       Price_unrealgain.neg = ifelse(!Price_unrealgainYN,10000000*Price_unrealgainAdj/PriceA,0))]
  
    } else if (return=="L") {
        DT[, `:=`(F_unrealgain.pos = ifelse(F_unrealgainYN,F_unrealgainAdj,0),
                       F_unrealgain.neg = ifelse(!F_unrealgainYN,F_unrealgainAdj,0),
                       Price_unrealgain.pos = ifelse(Price_unrealgainYN,Price_unrealgainAdj,0),
                       Price_unrealgain.neg = ifelse(!Price_unrealgainYN,Price_unrealgainAdj,0))]
    } else if (return=="NA") {
        DT<-DT
      } else {
        stop("L, P or NA")
        }

  
  DT  <- DT[, c("Ref_price", "IdxPriceA", "PriceA", "Price_unrealgainAdj", "F_unrealgainAdj", "F_unrealgain"):= NULL]


  }

DP2 <- function(x){format(round(x, 2),nsmall= 2)[1]}
DP4 <- function(x){format(round(x, 4),nsmall= 4)[1]}

BR4 <- function(x){paste0("(",DP4(x), ")")}
  
Quin_table <- function(object, sample){
    
    result <- data.table(tidy(object)) # requires library broom
    result <- result[, stars:= ifelse(p.value<.01, "***", ifelse(p.value<.05, "**", ifelse(p.value<.1, "*", "")))]
    Quin_summary <- fread(paste0("./Tables/Quin_summary_raw.", analysis, ".csv"))
    
    
    Quin_summary <- rbind(Quin_summary, data.table(Description=sample,Purchase_coeff=paste0(DP4(result[term=="Price_unrealgainYNTRUE",.(estimate)]),result[term=="Price_unrealgainYNTRUE",.(stars)]),
                                               Purchase_se=BR4(result[term=="Price_unrealgainYNTRUE",.(std.error)]), 
                                               Peak_coeff=paste0(DP4(result[term=="F_unrealgainYNTRUE",.(estimate)]),result[term=="F_unrealgainYNTRUE",.(stars)]), 
                                               Peak_se=BR4(result[term=="F_unrealgainYNTRUE",.(std.error)]),  
                                               Constant_coeff=paste0(DP4(result[term=="(Intercept)",.(estimate)]),result[term=="(Intercept)",.(stars)]), 
                                               Constant_se=BR4(result[term=="(Intercept)",.(std.error)])))
    
    fwrite(Quin_summary, paste0("./Tables/Quin_summary_raw.", analysis, ".csv"))
  }


QuinObs_table <- function(DT, sample){
  QuinObs_summary <- fread(paste0("./Tables/QuinObs_summary_raw.", analysis, ".csv"))
  QuinObs_summary <- rbind(QuinObs_summary, data.table(Description=sample, 
                                               Gain_purchase=DT[GainPurch>0, .N],  
                                               Loss_purchase=DT[GainPurch<0, .N], 
                                               Gain_peak=DT[GainPeak>0, .N], 
                                               Loss_peak=DT[GainPeak<0, .N]))
  
  fwrite(QuinObs_summary, paste0("./Tables/QuinObs_summary_raw.", analysis, ".csv"))
}



Both_ols1 <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Neighbour"
  # sample e.g. "Incl3yr", "Excl3yr"
  # format short (excl property characteristics) or long
  return <-"NA"
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)


ols_m4 <- felm(sold.dummy ~ Price_unrealgainYN + F_unrealgainYN + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

DT  <- DT[, c("Purch_duration","Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5") := NULL
          ][, c("Type", "New", "Quality100000", "Region"):= NULL]



ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + F_unrealgainYN |  0|0|  Property_Id + YrQtr, data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN |  0|0|  Property_Id + YrQtr, data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN |  0|0|  Property_Id + YrQtr, data =  DT, na.action=na.exclude)

Covariate_Labels <- c("Gain Since Purchase = 1", "Gain Since Peak = 1", "Years From Purchase", "Detached = 1", "Semi-detached = 1","Terraced = 1","New-build = 1", "Quality (£100,000)")


table <- stargazer(ols_m1, ols_m2, ols_m3, ols_m4, no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)", "(4)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "NO", "NO", "NO", "YES"), 
                             c( "Region", "NO", "NO", "NO", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_ols.tex"))

stargazer(ols_m1, ols_m2, ols_m3, ols_m4, no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)", "(4)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "NO", "NO", "NO", "YES"), 
                             c( "Region", "NO", "NO", "NO", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_ols.", out))
  
}


Both_ols2 <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Both"
  # sample e.g. Description if median spli must contain eith "high" or "low"
  # return whether calculated as alevel or %
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

 if (str_detect(sample, "high") | str_detect(sample, "low")){
   Quin_table(ols_m3, sample)
  QuinObs_table(DT, sample)
   }
# Where applicable update median split summary table

 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }


table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_ols.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_ols.", out))
    

}



Both_fe <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Neighbour"
  # sample e.g. "Incl3yr", "Excl3yr"
  # format short (excl property characteristics) or long
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)
  
  DT  <- DT[, c("Type", "New", "Quality100000", "Region"):= NULL]

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)


 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }

table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property FE", "YES", "YES", "YES"), 
                             c( "Quarter FE", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_fe.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property FE", "YES", "YES", "YES"), 
                             c( "Quarter FE", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_fe.", out))
    

}


Both_olsDecile <- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Both"
  # sample e.g. Description if median spli must contain eith "high" or "low"
  # return whether calculated as alevel or %
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Return_decile + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + Return_decile + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Return_decile + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 | Property_Id + YrQtr|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

 
 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }


table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Return Since Purchase Deciles", "YES", "YES", "YES"),
                             c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Return_decile", "Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title, sample, analysis, return, "Clean_ols.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Return Since Purchase Deciles", "YES", "YES", "YES"),
                             c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Return_decile","Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title, sample, analysis, return, "_ols.", out))
    

}


Both_olsQuintile<- function(dt, Ref_price, Ref_duration, title, sample, return){
  
  # title e.g. "Purchase", "Peak", "Both"
  # sample e.g. Description if median spli must contain eith "high" or "low"
  # return whether calculated as alevel or %
  
  
  DT <- regression_variables(dt, Ref_price, Ref_duration, return)

ols_m3 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m2 <- felm(sold.dummy ~ F_unrealgainYN + F_unrealgain.pos + F_unrealgain.neg  + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)

ols_m1 <- felm(sold.dummy ~ Price_unrealgainYN + Price_unrealgain.pos + Price_unrealgain.neg + Purch_duration + Purch_duration2 +  Purch_duration3 + Purch_duration4 + Purch_duration5 + Type + New + Quality100000 + Region| 0|0|  Property_Id + YrQtr , data =  DT, na.action=na.exclude)


 if (return=="P") {
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (%)",  "Return Since Purchase < 0 (%)", "Gain Since Peak = 1", "Return Since Peak > 0 (%)",  "Return Since Peak < 0 (%)", "Years From Purchase")
  
    } else if (return=="L") {
      
Quin_table(ols_m3, sample)
QuinObs_table(DT, sample)
      
Covariate_Labels <- c("Gain Since Purchase = 1", "Return Since Purchase > 0 (£100,000)",  "Return Since Purchase < 0 (£100,000)", "Gain Since Peak = 1", "Return Since Peak > 0 (£100,000)",  "Return Since Peak < 0 (£100,000)", "Years From Purchase")
} else {
        stop("L or P")
        }


table <- stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              float = F, omit.table.layout = "n", column.sep.width = "-15pt", table.layout="-dc#-tas-",
              type = "latex")


table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
write.table(table[11:(length(table)-2)], col.names = F, row.names = F, quote = FALSE, paste0("./Tables/LaTeX/", title,"Quin" ,sample, analysis, return, "Clean_ols.tex"))
  
stargazer(ols_m1, ols_m2, ols_m3,  no.space=TRUE, align=TRUE, digits = 4, digits.extra = 0,  dep.var.caption  = "",
            dep.var.labels = "Sale", object.names=FALSE, model.numbers=FALSE,  model.names=FALSE, column.labels = c("(1)", "(2)", "(3)"), 
              covariate.labels=Covariate_Labels,
            add.lines = list(c("Years From Purchase Quintics", "YES", "YES", "YES"), 
                             c( "Property Characteristics", "YES", "YES", "YES")),
              omit.stat=c("ser","f","adj.rsq"), omit=c("Purch_duration2", "Purch_duration3", "Purch_duration4", "Purch_duration5", "Type", "New", "Quality100000", "Region"), 
              font.size = "footnotesize", omit.table.layout = "n", column.sep.width = "-15pt", 
              type = filetype, out=paste0("./Tables/", title,"Quin" ,sample, analysis, return, "_ols.", out))
    

}


BinByReturnCI<- function(dt, lowlimit, highlimit, quartile, quartilename, multiplier){
  # dt - datatable
  # lowlimit - limit by minimum Unrealised gain, or  NA if no lower limit
  # highlimit - limit by maximum Unrealised gain, or  NA if no higher limit
  # quartile - subset by quartile or "All"
  # quartilename - name of quartile for faceting or "All"
  # multiplier - where applicable when partitioning data by say All_index proportion that be.g increased in value
  if(!is.na(lowlimit)) dt = dt[UnrealgainAdj>=lowlimit,] # 
  if(!is.na(highlimit)) dt = dt[UnrealgainAdj<=highlimit,] # 
  dt <- CutByReturnCI(dt, quartile, multiplier)
  dt <- dt[, .(bin.mean.gain=mean(UnrealgainAdj), proportion.sold=mean(sold.dummy), sum=sum(sold.dummy), length=length(sold.dummy), quartile=quartilename, bin.mean.duration=mean(DurationVal)), by=.(return.bin)][
    order(bin.mean.gain),]

  CI <- data.table(ci.binomial(dt[,sum], dt[,length]))
  dt <- cbind(dt, CI[,.(exact.lower95ci, exact.upper95ci)])
  
  } 


dispos_effect_tabCI <- function(DT, AdjUnrealGain, Valuation.duration, xlabel, xlimit, ylimit) {
  # Much smaller effect than purchase price or peak price so need to provide y axis limit and number of tick marks
  # quantile() in the CutByReturn function does not give 200 plotting points so have to adjyst the multiplier in BinByReturn to increase the number 
  data <- DT[, .(UnrealgainAdj = get(AdjUnrealGain), sold.dummy, DurationVal = get(Valuation.duration))][, DurationB := NA]
  data<-BinByReturnCI(data, lowlimit, highlimit, "All", "All", 1)}

if(analysis == "All1pc"){
  
  CutByReturnCI <-function(dt, quartile, multiplier) {
    # quartile - Either "All" or quartile name e.g. "1Q"
    if (quartile!="All") {
      dt <- dt[Region.bin==quartile,]
    }
    
    # Calculate the percentiles of return
    return.quantiles <- unique(quantile(dt[,UnrealgainAdj], probs=seq(0,1,.005/multiplier), na.rm=TRUE))
    # Bin by return percentile
    dt <- dt[,return.bin:=cut2(UnrealgainAdj, return.quantiles)]
    return(dt)
  }} else {CutByReturnCI <-function(dt, quartile, multiplier) {
    # quartile - Either "All" or quartile name e.g. "1Q"
    if (quartile!="All") {
      dt <- dt[Region.bin==quartile,]
    }
    
    # Calculate the percentiles of return
    return.quantiles <- unique(quantile(dt[,UnrealgainAdj], probs=seq(0,1,.005/multiplier), na.rm=TRUE))
    # Bin by return percentile
    dt <- dt[,return.bin:=cut2(UnrealgainAdj, return.quantiles, include.lowest=TRUE)]
    return(dt)
  }}




HPIindex2 <- function(dt, index, date, name) { 
  dt[, Date := as.Date(date, origin ="1970-01-01")] #  Date vector automatically converted to numeric so have to re-convert.
  
  
  
  
  
  setkey(dt,Date,District, Type)
  setkey(index,Date,District,Type)
  dt <- merge(dt,index, all.x=TRUE) # just using merge does not work
  setnames(dt, old= "Index", new=deparse(substitute(name))) # Can't use get() (need quotes in function call) here or deparse(substitute()) no quotes in function call
  dt[, Date:=NULL] # remove unnecessary column
  return(dt)
}

Revalue <- function(DT, End_Idx, Result) {
  # Assuming 'End_Idx', 'Idx_T', and 'IdxPriceA' are columns in DT
  # Replace with your actual column names
  
  # Calculate the result using the provided formula
  DT[, (Result) := (((.SD[[End_Idx]] - Idx_T) / Idx_T) + 1) * IdxPriceA, .SDcols = c(End_Idx, 'Idx_T', 'IdxPriceA')]
  
  # Return the modified data.table
  return(DT)
}

Mean0 <- function(x){format(round(mean(x, na.rm=TRUE), 0),nsmall= 0)[1]}
Mean2 <- function(x){format(round(mean(x, na.rm=TRUE), 2),nsmall= 2)[1]}

Odean_ttest <- function(Win, Lose){

  t_test <- t.test(Win, Lose)
  difference <- DP2(unname(t_test$estimate[1] - t_test$estimate[2]))
  if (t_test$p.value < 0.01) {
      stars <- "***"
    } else if (t_test$p.value < 0.05) {
      stars <- "**"
    } else if (t_test$p.value < 0.1) {
      stars <- "*"
    } else {
      stars <- ""
    }
  

  result <- c(DP2(unname(t_test$estimate[1])), DP2(unname(t_test$estimate[2])), paste0(difference, stars))
  
  }
  
Odean_table <- function(DT){
  
  T6 <- Odean_ttest(DT[Odean=="Sold_winner", UnrealgainT6_monthspc], DT[Odean=="Unsold_loser", UnrealgainT6_monthspc])
  T12 <- Odean_ttest(DT[Odean=="Sold_winner", UnrealgainT12_monthspc], DT[Odean=="Unsold_loser", UnrealgainT12_monthspc])
  T24 <- Odean_ttest(DT[Odean=="Sold_winner", UnrealgainT24_monthspc], DT[Odean=="Unsold_loser", UnrealgainT24_monthspc])
  
  Table <- rbind(cbind(Description=c("Average Return on Winning Homes Sold","Average Return on Paper Losses","Difference in Average Returns"), T6, T12, T24))
}

```





```{r cache=FALSE, eval=FALSE, echo= FALSE}
# Spreadsheet: Disposition-effect-checklist
# Overleaf: peak_prices_author_response.pdf. Writeup/Papers/Disposition_effect/Submission/R&R



```

```{r cache=FALSE, eval=TRUE, echo= FALSE}
# Run up to line 957 "Figures.Rmd" 

result <- dispos_effect_tabCI(Resold.indexed.regress,"UnrealgainAdj", "DurationVal", "Purchase", 0.005, .016)

ggplot(result[bin.mean.gain>-0.1 & bin.mean.gain < 0.2], aes(x = bin.mean.gain, y = proportion.sold)) +
  geom_line() # i.e. -£10,000 to +£20,000

ggplot(result[bin.mean.gain>-0.025 & bin.mean.gain < 0.05], aes(x = bin.mean.gain, y = proportion.sold)) +
  geom_line() # i.e. -£2,500 to +£5,000

ggplot(result[bin.mean.gain>-0.0 & bin.mean.gain < 0.03], aes(x = bin.mean.gain, y = proportion.sold)) +
  geom_line() # i.e. £0 to +£3,000

ggplot(result[bin.mean.gain>0.005 & bin.mean.gain < 0.02], aes(x = bin.mean.gain, y = proportion.sold)) +
  geom_line() # i.e. £500 to +£2,000
# Initially too many sales then flattens off between gain since purchase of 

```


```{r cache=FALSE, eval=TRUE, echo= FALSE}
# From data prepare

# pp_complete: https://www.gov.uk/government/statistical-data-sets/price-paid-data-downloads

All.clean <- fread("./Data/pp-complete202003.csv", col.names = c("TUI", "PriceA", "DoTA", "Postcode", "Type", "New", "Tenure", "PAON", "SAON", "Street", "Locality", "Town_City", "Old_District", "County", "Category", "Status"))

All.clean <-All.clean[Category=="A"]# exclude properties not Standard price paid entry

All.clean <-All.clean[PriceA>=250,]# exclude properties bought for less than £250

# Full address
All.clean <-All.clean[PAON!=""][(str_length(Postcode)>=6) & (str_length(Postcode)<=8)][order(Postcode, PAON, SAON),]# exclude properties with incomplete postcodes
Clean.District <- fread("./Data/Region_clean2003.csv", col.names = c("Old_District", "District", "Region", "Sub-region")) # Districts per PP database mapped to corrected admin districts. Admin districts change over time. HPI index is retrospectively updated but HMLR dataset isn't
# HPI dataset as at 
# set the ON clause as keys of the tables:
setkey(All.clean,Old_District)
setkey(Clean.District,Old_District)

# Maps original district names onto updated district names & regions 
All.clean  <- merge(All.clean,Clean.District[,.(Old_District, District, Region)], all.x=TRUE)
All.clean <- All.clean[order(TUI, DoTA)] 

All.clean <- All.clean[District !="N/A (split)",] 

All.clean <- All.clean[, .(TUI, DoTA, District)]

```

```{r cache=FALSE, eval=TRUE, echo= FALSE}

random_select <- function(data_table, num_to_select) {
  num_rows <- nrow(data_table)
  
  if (is.null(num_to_select) || num_to_select >= num_rows) {
    # Select all rows
    return(data_table)
  } else {
    # Randomly sample row indices
    sample_indices <- sample(1:num_rows, num_to_select, replace = FALSE)
    # Subset the data.table based on the sampled indices
    return(data_table[sample_indices, ])
  }
}

num_to_select <- NULL# NULL if all

Sold_winner_peak <- Resold.indexed.regress[SoldInPeriod==TRUE & Peak.unrealgainYN==TRUE,]
Unsold_loser_peak <- Resold.indexed.regress[SoldInPeriod==FALSE & Peak.unrealgainYN==FALSE,]  


Sold_winner_peak <- random_select(Sold_winner_peak, num_to_select)
Unsold_loser_peak <- random_select(Unsold_loser_peak, num_to_select)

Odean_peak <- rbind(cbind(Sold_winner_peak, Odean="Sold_winner"), cbind(Unsold_loser_peak, Odean="Unsold_loser"))



# set the ON clause as keys of the tables:
setkey(Odean_peak,TUI)
setkey(All.clean,TUI)

# perform the join using the merge function
Odean_peak <- merge(Odean_peak,All.clean, all.x=TRUE)


Odean_peak <- Odean_peak[, DoTA := as.Date(DoTA)
               ][order(TUI, date.val, -DoTA)
                 ][, .SD[1], by = .(TUI, date.val)
                   ][, `:=` (T_6_months = date.val + months(6),
                             T_12_months = date.val + years(1),
                             T_24_months = date.val + years(2))]





Sold_winner_price <- Resold.indexed.regress[SoldInPeriod==TRUE & UnrealgainYN==TRUE,]
Unsold_loser_price <- Resold.indexed.regress[SoldInPeriod==FALSE & UnrealgainYN==FALSE,]  


Sold_winner_price <- random_select(Sold_winner_price, num_to_select)
Unsold_loser_price <- random_select(Unsold_loser_price, num_to_select)

Odean_price <- rbind(cbind(Sold_winner_price, Odean="Sold_winner"), cbind(Unsold_loser_price, Odean="Unsold_loser"))


# set the ON clause as keys of the tables:
setkey(Odean_price,TUI)
setkey(All.clean,TUI)

# perform the join using the merge function
Odean_price <- merge(Odean_price,All.clean, all.x=TRUE)


Odean_price <- Odean_price[, DoTA := as.Date(DoTA)
               ][order(TUI, date.val, -DoTA)
                 ][, .SD[1], by = .(TUI, date.val)
                   ][, `:=` (T_6_months = date.val + months(6),
                             T_12_months = date.val + years(1),
                             T_24_months = date.val + years(2))]






All.clean <- NULL
```


```{r cache=FALSE, eval=TRUE, echo= FALSE}

HPIndex <- fread("./Data/UK-HPI-full-file-2020-03.csv")

# The March 2020 HPI omitted the 2018 indices for the combined BOURNEMOUTH CHRISTCHURCH AND POOLE district. As of 21/07/2020 an updated HPI has not been updated. Indices for the former separate districts of BOURNEMOUTH, CHRISTCHURCH AND POOLE were published in the January 2020 HPI.
HPIndexBCP <- fread("./Data/UK-HPI-full-file-2020-01.csv")
HPIndexBCP <- HPIndexBCP[RegionName %in% c("Bournemouth", "Christchurch", "Poole"),]
HPIndex <- rbind(HPIndex, HPIndexBCP)

# Index by month, district, property type
HPI <- HPIndex[, .(Date, District =toupper(RegionName), Detached = DetachedIndex, Semi = SemiDetachedIndex, Terraced = TerracedIndex, Flat=  FlatIndex)]
HPI <- melt(setDT(HPI), id.vars = c("Date","District"), variable.name = "Type", value.name = "Index")
HPI <-HPI[, `:=`(Date = as.Date(Date, "%d/%m/%Y"))]



HPIndex20_22 <- fread("http://publicdata.landregistry.gov.uk/market-trend-data/house-price-index-data/UK-HPI-full-file-2022-09.csv?utm_medium=GOV.UK&utm_source=datadownload&utm_campaign=full_fil&utm_term=9.30_16_11_22")
HPIndex20_22 <- HPIndex20_22[, `:=`(Date = as.Date(Date, "%d/%m/%Y"))][Date > "2020-03-01" & Date <= "2022-01-01", ]


HPIndex20_22 <- HPIndex20_22[, .(Date, District =toupper(RegionName), Detached = DetachedIndex, Semi = SemiDetachedIndex, Terraced = TerracedIndex, Flat=  FlatIndex)]
HPIndex20_22 <- melt(setDT(HPIndex20_22), id.vars = c("Date","District"), variable.name = "Type", value.name = "Index")

BCP <- HPIndex20_22[District=="BOURNEMOUTH CHRISTCHURCH AND POOLE",]
BOURNEMOUTH <- copy(BCP)
BOURNEMOUTH[, District:="BOURNEMOUTH"]
CHRISTCHURCH <- copy(BCP)
CHRISTCHURCH[, District:="CHRISTCHURCH"] 
POOLE <- copy(BCP)
POOLE[, District:="POOLE"] 
BCP <- rbind(BCP, BOURNEMOUTH, CHRISTCHURCH, POOLE)

BUCKINGHAMSHIRE <- HPIndex20_22[District=="BUCKINGHAMSHIRE",]
AYLESBURY <- copy(BUCKINGHAMSHIRE)
AYLESBURY[, District:="AYLESBURY VALE"]
CHILTERN <- copy(BUCKINGHAMSHIRE)
CHILTERN[, District:="CHILTERN"]
SBUCKS <- copy(BUCKINGHAMSHIRE)
SBUCKS[, District:="SOUTH BUCKS"]
WYCOMBE <- copy(BUCKINGHAMSHIRE)
WYCOMBE[, District:="WYCOMBE"]
BUCKINGHAMSHIRE <- rbind(BUCKINGHAMSHIRE, AYLESBURY, CHILTERN, SBUCKS, WYCOMBE)


NORTHNORTHAMPTON <- HPIndex20_22[District=="NORTH NORTHAMPTONSHIRE",]
CORBY <- copy(NORTHNORTHAMPTON)
CORBY[, District:="CORBY"]
EASTNORTHAMPTON <- copy(NORTHNORTHAMPTON)
EASTNORTHAMPTON[, District:="EAST NORTHAMPTONSHIRE"]
KETTERING <- copy(NORTHNORTHAMPTON)
KETTERING[, District:="KETTERING"]
WELLINGBOROUGH <- copy(NORTHNORTHAMPTON)
WELLINGBOROUGH[, District:="WELLINGBOROUGH"]
NORTHNORTHAMPTON <- rbind(NORTHNORTHAMPTON, CORBY, EASTNORTHAMPTON, KETTERING, WELLINGBOROUGH)

WESTNORTHAMPTON <- HPIndex20_22[District=="WEST NORTHAMPTONSHIRE",]
DAVENTRY <- copy(WESTNORTHAMPTON)
DAVENTRY[, District:="DAVENTRY"]
NORTHAMPTON <- copy(WESTNORTHAMPTON)
NORTHAMPTON[, District:="NORTHAMPTON"]
SOUTHNORTHAMPTON <- copy(WESTNORTHAMPTON)
SOUTHNORTHAMPTON[, District:="SOUTH NORTHAMPTONSHIRE"]

WESTNORTHAMPTON <- rbind(WESTNORTHAMPTON, DAVENTRY, NORTHAMPTON, SOUTHNORTHAMPTON)

HPI <- rbind(HPI, HPIndex20_22, BCP, BUCKINGHAMSHIRE, NORTHNORTHAMPTON, WESTNORTHAMPTON)



```




```{r cache=FALSE, eval=TRUE, echo= FALSE}
Odean_peak <- HPIindex2(Odean_peak, HPI, Odean_peak[, date.val], Idx_T)
Odean_peak <- HPIindex2(Odean_peak, HPI, Odean_peak[, T_6_months], Idx_T6)
Odean_peak <- HPIindex2(Odean_peak, HPI, Odean_peak[, T_12_months], Idx_T12)
Odean_peak <- HPIindex2(Odean_peak, HPI, Odean_peak[, T_24_months], Idx_T24)

Odean_peak <- Revalue(Odean_peak, "Idx_T6", "Revalue_T6")
Odean_peak <- Revalue(Odean_peak, "Idx_T12", "Revalue_T12")
Odean_peak <- Revalue(Odean_peak, "Idx_T24", "Revalue_T24")

Odean_peak<-Odean_peak[, `:=` (UnrealgainT6_months = Revalue_T6 - IdxPriceA,
                     UnrealgainT12_months = Revalue_T12 - IdxPriceA,
                     UnrealgainT24_months = Revalue_T24 - IdxPriceA)
             ][,`:=` (UnrealgainT6_monthspc = UnrealgainT6_months/IdxPriceA *100,
                     UnrealgainT12_monthspc = UnrealgainT12_months/IdxPriceA *100,
                     UnrealgainT24_monthspc = UnrealgainT24_months/IdxPriceA *100)]

Odean_peakNA<-Odean_peak[is.na(UnrealgainT24_monthspc)==TRUE, .(Old_district=District)
               ][order(Old_district)]
Odean_peakNA <- unique(Odean_peakNA, by = "Old_district")
           

fwrite(Odean_peakNA, "./Data/Odean_peakNA_NewDistrict.csv")



Peak_ttest <- rbind(c("Returns Since Peak Price Date (%)", "","",""), Odean_table(Odean_peak))

Peak_ttest_dt <- data.table(Odean_table(Odean_peak))
Peak_ttest_lt <- xtable(Peak_ttest_dt, include.rownames = FALSE, include.colnames = FALSE)
print(Peak_ttest_lt, file = paste0("./Tables/LaTeX/Odean_table_peak", analysis, ".tex"))

#NEED TO REMOVWE TOP, BOTTOM AND COLUMN NUMBERS OF LaTeX OUTPUT

```




```{r cache=FALSE, eval=TRUE, echo= FALSE}
Odean_price <- HPIindex2(Odean_price, HPI, Odean_price[, date.val], Idx_T)
Odean_price <- HPIindex2(Odean_price, HPI, Odean_price[, T_6_months], Idx_T6)
Odean_price <- HPIindex2(Odean_price, HPI, Odean_price[, T_12_months], Idx_T12)
Odean_price <- HPIindex2(Odean_price, HPI, Odean_price[, T_24_months], Idx_T24)

Odean_price <- Revalue(Odean_price, "Idx_T6", "Revalue_T6")
Odean_price <- Revalue(Odean_price, "Idx_T12", "Revalue_T12")
Odean_price <- Revalue(Odean_price, "Idx_T24", "Revalue_T24")

Odean_price<-Odean_price[, `:=` (UnrealgainT6_months = Revalue_T6 - IdxPriceA,
                     UnrealgainT12_months = Revalue_T12 - IdxPriceA,
                     UnrealgainT24_months = Revalue_T24 - IdxPriceA)
             ][,`:=` (UnrealgainT6_monthspc = UnrealgainT6_months/IdxPriceA *100,
                     UnrealgainT12_monthspc = UnrealgainT12_months/IdxPriceA *100,
                     UnrealgainT24_monthspc = UnrealgainT24_months/IdxPriceA *100)]

Odean_priceNA<-Odean_price[is.na(UnrealgainT24_monthspc)==TRUE, .(Old_district=District)
               ][order(Old_district)]
Odean_priceNA <- unique(Odean_priceNA, by = "Old_district")
           

fwrite(Odean_priceNA, "./Data/Odean_priceNA_NewDistrict.csv")



Price_ttest <- rbind(c("Returns Since Purchase (%)", "","", ""), Odean_table(Odean_price)) 

Price_ttest_dt <- data.table(Odean_table(Odean_price))
Price_ttest_lt <- xtable(Price_ttest_dt, include.rownames = FALSE, include.colnames = FALSE)
print(Price_ttest_lt, file = paste0("./Tables/LaTeX/Odean_table_price", analysis, ".tex"))

#NEED TO REMOVWE TOP, BOTTOM AND COLUMN NUMBERS OF LaTeX OUTPUT
```



```{r cache=FALSE, eval=TRUE, echo= FALSE}

Odean_table <- data.table(rbind(Price_ttest, c(".","","",""), Peak_ttest))

print(Odean_table)

fwrite(Odean_table, paste0("./Tables/Odean_table", analysis, ".csv"))
fwrite(Price_ttest, paste0("./Tables/Odean_table_price", analysis, ".csv"))
fwrite(Peak_ttest, paste0("./Tables/Odean_table_peak", analysis, ".csv"))


Odean_peak<-NULL
Odean_price<-NULL
Unsold_loser_peak<-NULL
Sold_winner_peak<-NULL
Unsold_loser_price<-NULL
Sold_winner_price<-NULL
HPI <- NULL
HPIndex <- NULL
HPIndex20_22<- NULL

```

```{r}

LSampleSelect(paste0("./Tables/Odean_table", analysis, ".csv"))


